/**
 * This class creates a TUI for loans. It also sends all the input provided by the user to LoanCtr class and checks the correctness of the provided inputs.
 * 
 */

package TUILayer;
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
public class LoanUI
{
    private LoanCtr loanCtr;
    

    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI(ControlCollector collector)
    {
        loanCtr = collector.getL();
    }
    
    /**
     * This method calls up the menu for the Loan TUI
     */
    public void start()
    {
        loanMenu();
    }
    
    /**
     * This method prints a loan menu and calls up other functions depending on the input provided by the user.
     */
    private void loanMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeLoanMenu(); 
          if(choice == 1)
          {   //show person
              getLoan();
          }
          else{
              if(choice == 2){
                    lendDVD();
              }  
              else{
                  if(choice == 3){
                      returnDVD();
                      //delete person
                  }  
                  else{
                      if(choice == 4){
                          // update person
                          reserveDVD();
                      }
                      else{
                          if(choice == 5){
                               listAllLoans();
                          }
                          else{
                               exit = true;                              
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    /**
     * This method provides an input for printing to the loanMenu() method
     */
    private int writeLoanMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about a loan");
            System.out.println(" (2) Lend DVD");
            System.out.println(" (3) Return DVD");
            System.out.println(" (4) Reserve DVD for a friend");
            System.out.println(" (5) List all loans data");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    /**
     * This method collects input which is an id of a borrower, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */
    private int inputBorrowerId()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write the ID of the person who borrows the DVD:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    /**
     * This method collects input, which is an id of loan, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */
    private int inputLoanId()
       {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write the ID of the loan, the ID is unique for all loans:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    /**
     * This method collects input, which is an id of dvd, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */
    private int inputDVDId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write the id of the DVD you wish to lend:");
        int id = keyboard.nextInt();
        return id;
    }
    
    /**
     * This method collects input, which is an id of copy of a dvd, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */
    private int inputCopyId()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write the ID of the copy you wish to lend:");
        int id = keyboard.nextInt();
        return id;
    }
    
    /**
     * This method collects input, which is an date on which the copy was borrowed, from the user.
     */
    private String inputBorrowDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write the date of borrowing: ");
        return keyboard.nextLine();
    }
    
    /**
     * This method collects input, which is a period for which the copy is borrowed, from the user.
     */
    private String inputPeriod()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write the period of the loan");
        String period = keyboard.nextLine();
        return period;
       
    }
    
     /**
     * This method collects input, which is an id the person reserving the DVD, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */
    private int inputReserverId()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write the ID of the person wishes to reserve:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    /**
     * This method prints the information about desired loan.
     * If the loan with the desired ID doesn't exist it prints an error statement.
     */
    private void getLoan()
    {
        int id = inputLoanId();
        if(loanCtr.isThere(id))
        {
            loanCtr.getLoan(id);
        }
        else
        {
            System.out.println("There is no such loan");
        }
        waitForInput();
    }
    
    /**
     * This method first collects the input (id of the borrower, id of the to-be-created loan, id of the DVD which copy is suppose to be lended, id of the lended copy,
     * date of borrowing and finally the period for which the DVD is lended. It also checks if person with desired ID exists in the address book,
     * if copy and DVD with provided IDs exist and whether the DVD has been reserved and if so, then if it was done by the person we now wish to lend this DVD to.
     * If any of those checks is not passed, the method terminates and user returns to the Loan menu.
     */
    private void lendDVD()
    {
        boolean ok = false;

        int borrowerId = inputBorrowerId();
        if (loanCtr.accessAdd().isIt(borrowerId))
        {
            int loanId = inputLoanId();
            if (loanCtr.isThere(loanId))
            {
                System.out.println("The loan with such ID already exists");
            }
            else
            {
                int dvdId = inputDVDId();
                if(loanCtr.accessDVD().isThere(dvdId))
                {
                    int copyId = inputCopyId();
                    if(loanCtr.accessDVD().hasCopy(dvdId, copyId))
                    {
                        if(loanCtr.accessDVD().isAvailable(dvdId, copyId))
                        {
                            if(loanCtr.checkReservation(borrowerId, dvdId, copyId))
                            {
                                String borrowDate = inputBorrowDate();
                                String period = inputPeriod();
                                loanCtr.lendDVD(borrowerId, loanId, dvdId, copyId, borrowDate, period);                                
                            }
                            else
                            {
                                System.out.println("This DVD is reserved for someone else");
                            }                            

                        }
                        else
                        {
                            System.out.println("This copy of DVD has already been borrowed");
                        }
                    }
                    else
                    {
                        System.out.println("There is no copy of that DVD with this ID");
                    }
                }     
                else
                {
                    System.out.println("There is no DVD with this ID");
                }
            }
        }
        else
        {
            System.out.println("There is no person with this ID in an address book,"
            + "so you cannot loan a DVD to him/her");
        }
        waitForInput();
        
    }
    
    /**
     * This method uses the input (id of the borrower and id of the loan) to delete the loan and make a copy available once again.
     */
    private void returnDVD()
    {
        int borrowerId = inputBorrowerId();
        if (loanCtr.accessAdd().isIt(borrowerId))
        {
            int loanId = inputLoanId();
            if (loanCtr.isThere(loanId))
            {
                loanCtr.returnDVD(borrowerId, loanId);
            }
            else
            {
                System.out.println("There is no loan with this ID");
            }
        }
        else
        {
            System.out.println("There is no such person in the address book");
        }
        waitForInput();
                
        
    }
    
    /**
     * This method reserves a DVD copy, so it can be borrowed only by the person who reserved it. If the DVD is currently unavailable,
     * after it is returned it will be possible to lend it only to the person who made a reservation.
     */
    private void reserveDVD()
    {
        int reserverId = inputReserverId();
        if (loanCtr.accessAdd().isIt(reserverId))
        {
            int dvdId = inputDVDId();
            if (loanCtr.accessDVD().isThere(dvdId))
            {
                int copyId = inputCopyId();
                if(loanCtr.accessDVD().hasCopy(dvdId, copyId))
                {
                    loanCtr.reserveDVD(reserverId, dvdId, copyId);
                }
                else
                {
                    System.out.println("There is no copy of that DVD with this ID");
                }
            }     
            else
            {
                System.out.println("There is no DVD with this ID");
            }
        }
        else
        {
            System.out.println("There is no person with this ID in an address book,"
            + "so you cannot loan a DVD to him/her");
        } 
        waitForInput();

                    
        
    }
    
    /**
     * This method prints out information about all loans.
     */
    private void listAllLoans()
    {
        loanCtr.listAllLoans();
        waitForInput();
    }
    
    /**
     * This method makes the TUI wait for ENTER so it is possibble to read all the data on the screen.
     */
    private void waitForInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nPress ENTER to continue\n");
        keyboard.nextLine();
    }
    
    


}
